package com.example.myapplication.base.network;

import com.example.myapplication.BuildConfig;
import com.example.myapplication.base.network.cookie.CookieJarImpl;
import com.example.myapplication.base.network.cookie.MyCookieMannager;
import com.example.myapplication.base.network.interceptor.BaseUrlInterceptor;
import com.example.myapplication.base.network.interceptor.CacheInterceptor;
import com.example.myapplication.base.network.interceptor.ParamsInterceptor;
import com.example.myapplication.base.network.retrofit.ResponseConverterFactory;
import com.jakewharton.retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import ohos.app.Context;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;

import java.util.concurrent.TimeUnit;

import static com.example.myapplication.constants.EnvironmentConstants.BASE_URL;

public class NetWorkManager {

    public static NetWorkManager netWorkManager;

    private Retrofit retrofit;

    private OkHttpClient okHttpClient;

    private String baseUrl = BASE_URL;


    private Context context;

    public void init(Context context) {
        this.context = context;
    }

    public static NetWorkManager getInstance() {
        if (netWorkManager == null) {
            synchronized (NetWorkManager.class) {
                if (netWorkManager == null)
                    netWorkManager = new NetWorkManager();
            }

        }
        return netWorkManager;
    }

    public OkHttpClient getOkHttpClient() {
        if (okHttpClient == null) {
            OkHttpClient.Builder builder = new OkHttpClient.Builder()
                    .connectTimeout(10, TimeUnit.SECONDS)
                    .writeTimeout(10, TimeUnit.SECONDS)
                    .readTimeout(30, TimeUnit.SECONDS)
                    .cookieJar(new CookieJarImpl(MyCookieMannager.getInstance(context)))
//                .cache(cache)
//                .sslSocketFactory(SSLFactoryUtil.getSocketFactory(), SSLFactoryUtil.getTrustManager())
                    .hostnameVerifier((hostname, session) -> true)
                    .addInterceptor(new BaseUrlInterceptor())
                    .addInterceptor(new ParamsInterceptor())
                    .addInterceptor(new CacheInterceptor());  //添加 paramsInterceptor

            if (BuildConfig.DEBUG) {
                //打印请求相关的信息
                HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
                logging.setLevel(HttpLoggingInterceptor.Level.BODY);

                builder.addInterceptor(logging);
            }
            okHttpClient = builder.build();
        }
        return okHttpClient;
    }

    public Retrofit getRetrofit() {
        if (okHttpClient == null)
            okHttpClient = getOkHttpClient();
        if (retrofit == null)
            retrofit = new Retrofit.Builder()
                    .baseUrl(baseUrl)
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) // 添加Rx适配器
                    .addConverterFactory(ResponseConverterFactory.create()) // 添加Gson转换器
                    .client(okHttpClient)
                    .build();
        return retrofit;
    }
}
